<script setup lang="ts">
import Node from "./base/Node.vue";
import { computed, ref } from "vue";
import nodeMixin from "../NodeMixin";
import type { FlowModelNode } from "../ProcessDef";

const props = defineProps({
  ...nodeMixin.props
});
const emit = defineEmits(["insertNode", "delete", "select"]);
const _value = defineModel<FlowModelNode>();
const showErr = ref(false);
const errInfo = ref(null);

const content = computed(() => {
  let desc = "请设置节点及任务属性";
  const prop = _value.value.props;
  switch (prop.mode) {
    case "AUTO_REFUSE":
      desc = "系统自动拒绝流程🚫";
      break;
    case "AUTO_PASS":
      desc = "系统自动通过流程✔️";
      break;
    case "USER":
      switch (prop.ruleType) {
        case "ASSIGN_USER":
          if (prop.assignUser.length === 0) {
            desc = "请指定审批人";
          } else {
            desc = prop.assignUser.map(v => v.name).join("、");
          }
          break;
        case "ROOT_SELECT":
          desc = "发起人提交流程时指定人";
          break;
        case "ROOT_SELF":
          desc = "发起人自己审批";
          break;
        case "LEADER":
          if (prop.leader.level > 1) {
            desc = `发起人的第${prop.leader.level}级部门主管`;
          } else {
            desc = "发起人的直属主管";
          }
          break;
        case "LEADER_TOP":
          desc = "逐级部门主管审批";
          break;
        case "ASSIGN_DEPT":
          desc = `部门[${prop.assignDept.dept.map(v => v.name).join("、")}]的主管审批`;
          break;
        case "ASSIGN_ROLE":
          desc = `具有角色[${prop.assignRole.map(v => v.name).join("、")}]的人员审批`;
          break;
      }
      break;
  }
  return desc;
});

/**
 * 节点规则校验函数
 * @param errs 错误信息
 */
function validate(errs) {
  const prop = _value.value.props;
  if (prop.tasks.length > 0) {
    prop.tasks.forEach(item => {
      switch (item.jobType) {
        case "0":
          if (!item.userId && (!item.userList || item.userList.length < 1)) {
            errInfo.value = `${item.name}未指定具体审批人`;
            errs.push(`${_value.value.name}的${item.name}未指定审批人`);
            return;
          }
          break;
        case "1":
          if (!item.roleIds && (!item.roleList || item.roleList.length < 1)) {
            errInfo.value = `${item.name}未设置审批角色`;
            errs.push(`${_value.value.name}的${item.name}未设置角色`);
            return;
          }
          break;
        case "3":
          break;
        case "4":
          if (item.dynamicUserType === "1") {
            if (!item.dynamicUserId) {
              errInfo.value = `${item.name}未设置指定用户主管`;
              errs.push(`${_value.value.name}的${item.name}未设置指定用户主管`);
              return;
            }
          }
          break;
        case "5":
          if (item.dynamicUserType === "1") {
            if (!item.dynamicUserId) {
              errInfo.value = `${item.name}未设置指定用户主管`;
              errs.push(`${_value.value.name}的${item.name}未设置指定用户主管`);
              return;
            }
          }
          break;
        case "6":
          if (!item.dynamicDeptId) {
            errInfo.value = `${item.name}未设置指定部门主管`;
            errs.push(`${_value.value.name}的${item.name}未设置指定部门主管`);
            return;
          }
          break;
        case "7":
          if (!item.dynamicUserKey) {
            errInfo.value = `${item.name}未设置动态办理人表单字段`;
            errs.push(`${_value.value.name}的${item.name}未设置动态办理人表单字段`);
            return;
          }
          break;
      }
    });
  }
  if (errs.length > 0) {
    showErr.value = true;
  } else {
    showErr.value = false;
  }
  return;
}

const handleNodeSelect = taskIndex => {
  if (!taskIndex) {
    taskIndex = 0;
  }
  emit("select", { node: _value.value, taskIndex: taskIndex });
};

defineExpose({ validate });
</script>

<template>
  <node
    v-model="_value"
    :readonly="props.readonly"
    :show-error="showErr"
    :error-info="errInfo"
    :index="index"
    type="Job"
    :header-color="props.headerColor"
    :flowHistory="flowHistory"
    header-icon="stamp"
    :content="content"
    @select="handleNodeSelect"
    @insertNode="type => emit('insertNode', props.branch, props.index, type)"
    @delete="emit('delete', props.branch, props.index)"
  />
</template>

<style scoped></style>
